<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="75px">
      <el-form-item label="项目名称:" prop="ktName">
        <el-input size="mini"
                  v-model="queryParams.ktName"
                  placeholder="请输入项目名称"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目编号:" prop="ktBh">
        <el-input size="mini"
                  v-model="queryParams.ktBh"
                  placeholder="请输入项目编号"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申报单位:" prop="zrBm">
        <el-input size="mini"
                  v-model="queryParams.zrBm"
                  placeholder="请输入申报单位"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目年度:" prop="year">
        <el-select filterable v-model="queryParams.year" placeholder="请选择项目年度" clearable size="mini">
          <el-option
            v-for="dict in dict.type.year"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目状态:" prop="processStatus">
        <el-select v-model="queryParams.processStatus" placeholder="请选择项目状态" clearable size="mini">
          <el-option
            v-for="dict in dict.type.process_business_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课题归属:" prop="ktGs">
        <el-select filterable v-model="queryParams.ktGs" placeholder="请选择课题归属" clearable size="mini">
          <el-option
            v-for="dict in dict.type.kylx_ktgs"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="依托工程:" prop="ytGc">
        <el-input size="mini"
                  v-model="queryParams.ytGc"
                  placeholder="请输入依托工程"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-dropdown trigger="click">
          <el-button type="primary" icon="el-icon-edit" :disabled="single">
            修改项目状态<i style="color: #d9e5f3;" class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleSubmit('在研')"><span style="font-size: 16px">在研</span>
            </el-dropdown-item>
            <el-dropdown-item @click.native="handleSubmit('结题')"><span style="font-size: 16px">结题</span>
            </el-dropdown-item>
            <el-dropdown-item @click.native="handleSubmit('暂停')"><span style="font-size: 16px">暂停</span>
            </el-dropdown-item>
            <el-dropdown-item @click.native="handleSubmit('终止')"><span style="font-size: 16px">终止</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="vxe-icon-file-excel"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kylx:xmsb:export']"
        >导出Excel
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="vxe-icon-file-word"
          size="mini"
          :disabled="single"
          @click="handleExports"
        >导出docx
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table
      @row-click="clickRow" ref="table"
      highlight-current-row
      border
      :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
      v-loading="loading"
      :data="xmsbList"
      @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" width="50"></el-table-column>
      <!--      <el-table-column label="二维码" align="center">-->
      <!--        <template slot-scope="scope">-->
      <!--          <vueQr :text="scope.row.ktName" :size="120"></vueQr>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column show-overflow-tooltip label="年度" align="center" prop="year" v-if="columns[0].visible"
                       width="70">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.year" :value="scope.row.year"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目编号" align="center" prop="ktBh" v-if="columns[1].visible"/>
      <el-table-column show-overflow-tooltip label="项目名称" align="center" prop="ktName" v-if="columns[2].visible">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.ktName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="申报单位" align="center" prop="zrBm" v-if="columns[4].visible"/>
      <el-table-column show-overflow-tooltip label="依托工程" align="center" prop="ytGc"/>
      <el-table-column show-overflow-tooltip label="项目总额(万)" align="center" prop="xmze" v-if="columns[6].visible"
                       width="100"/>
      <el-table-column show-overflow-tooltip label="申请资助(万)" align="center" prop="sqzz" v-if="columns[7].visible"
                       width="100"/>
      <el-table-column show-overflow-tooltip label="课题归属" align="center" prop="ktGs" v-if="columns[3].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kylx_ktgs" :value="scope.row.ktGs"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="计划开始时间" align="center" prop="qTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.qTime, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="计划完成时间" align="center" prop="zTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.zTime, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目延期" align="center" prop="yqCount">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleXmYqCk(scope.row)"
          >
            <el-link v-if="scope.row.yqCount != null" type="primary">{{ '查看' }}</el-link>
          </el-button>
          <el-link type="danger" style="color: #0D1623" disabled v-if="scope.row.yqCount == null">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="录入人员" align="center" prop="lrRy" v-if="columns[10].visible"/>
      <el-table-column show-overflow-tooltip label="项目状态" align="center" prop="xmZt"/>
      <el-table-column show-overflow-tooltip label="批准文号" align="center" prop="lxwjId">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handlePzWhButtonClick(scope.row)"
          >
            <el-link type="primary" v-if="scope.row.pzWh != null">{{ scope.row.pzWh }}</el-link>
          </el-button>
          <el-link type="danger" disabled v-if="scope.row.pzWh == null">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="合同信息" align="center" prop="htbzId">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleHtXxCk(scope.row)"
          >
            <el-link v-if="scope.row.htbzId != null" type="primary">{{ '查看' }}</el-link>
          </el-button>
          <el-link type="danger" disabled v-if="scope.row.htbzId == null">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="合同变更信息" align="center" prop="htBgCount">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleHtBgCk(scope.row)"
          >
            <el-link v-if="scope.row.htBgCount != null" type="primary">{{ '查看' }}</el-link>
          </el-button>
          <el-link type="danger" style="color: #0D1623" disabled v-if="scope.row.htBgCount == null">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="专利申请" align="center" prop="zlsbCount">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleZlSqCk(scope.row)"
          >
            <el-link v-if="scope.row.zlsbCount != null" type="primary">{{ '查看' }}</el-link>
          </el-button>
          <el-link type="danger" style="color: #0D1623" disabled v-if="scope.row.zlsbCount == null">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="企业工法" align="center" prop="qygfCount">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleQyGfCk(scope.row)"
          >
            <el-link v-if="scope.row.qygfCount != null" type="primary">{{ '查看' }}</el-link>
          </el-button>
          <el-link type="danger" style="color: #0D1623" disabled v-if="scope.row.qygfCount == null">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="流程" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleJinDu(scope.row)"
            v-if="scope.row.processStatus !== 1"
          >查询进度
          </el-button>
          <el-link type="danger" style="font-size: 11px" disabled v-else>{{ '未提交审批' }}</el-link>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    >
    </pagination>

    <!-- 查询项目申报(主表 + 子表) 弹窗 -->
    <Xmxq ref="xmxq"/>

    <!-- 查看流程弹窗 -->
    <Jindu ref="picture"/>

    <!-- 立项文件详情组件 -->
    <lx-wj-xq ref="lxWjXqPicture"/>

    <!-- 项目延期查看 -->
    <XmYqCk ref="xmYqCk"/>

    <!-- 合同信息查看 -->
    <HtXx ref="htXx"/>

    <!-- 合同变更查看 -->
    <HtBgCk ref="htBgCk"/>

    <!-- 专利申请查看 -->
    <ZlSqCk ref="zlSqCk"/>

    <!-- 企业工法查看 -->
    <QyGfCk ref="qyGfCk"/>

  </div>
</template>

<script>
import {exportXmsbWord, listXmsb, updateXmsb} from "@/api/xmgl/lxgl/xmsb/xmsb";
import {getToken} from "@/utils/auth";
import {quillEditor} from 'vue-quill-editor';
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import FillingInstructions from "@/views/xmgl/lxgl/xmsb/components/fillingInstructions";
import Xmxq from "@/views/xmgl/lxgl/xmsb/components/xmxq";
import XmsbSc from "@/views/xmgl/lxgl/xmsb/components/xmsbSc";
import KjryDx from "@/views/components/kjryXz/kjryDx";
import vueQr from "vue-qr";
import Jindu from "@/views/components/liuChengState/jindu.vue";
import LxWjXq from "@/views/xmgl/lxgl/xmcx/components/lxWjXq";
import XmYqCk from "@/views/xmgl/htgl/htbz/components/XmYqCk";
import HtXx from "@/views/xmgl/lxgl/xmcx/components/HtXx";
import HtBgCk from "@/views/xmgl/htgl/htbz/components/HtBgCk";
import ZlSqCk from "@/views/xmgl/lxgl/xmcx/components/ZlSqCk";
import QyGfCk from "@/views/xmgl/lxgl/xmcx/components/QyGfCk.vue";

export default {
  name: "Xmcx",
  components: {
    QyGfCk,
    ZlSqCk, HtBgCk, HtXx, XmYqCk, LxWjXq, Jindu, KjryDx, XmsbSc, Xmxq, FillingInstructions, Treeselect, vueQr
  },
  comments: {quillEditor},
  dicts: ['kylx_fjlx', 'process_business_status', 'kylx_zcry_ktfg', 'year', 'kylx_ssly_cwxt', 'kylx_gqfw', 'kylx_country', 'sys_user_sex', 'kylx_xmfl_cwxt', 'kylx_ssly', 'kylx_hzfs', 'rcpt_zc', 'kylx_yfxmjb', 'kylx_ktgs', 'kylx_ktfl', 'kylx_bz'],
  provide() {
    return {};
  },
  updated() {
  },
  data() {
    return {
      loadings: false,
      activeName: 'xmjbxx', //默认打开的标签
      allFilesUploaded: false, // 添加这个来追踪所有文件是否上传完成
      // 中间试验费子表选中数据
      checkedLxglXmsbZjsyf: [],
      // 研发设备子表选中数据
      checkedLxglXmsbYfsb: [],
      // 研发材料子表选中数据
      checkedLxglXmsbYfcl: [],
      // 课题组成人员子表选中数据
      checkedLxglXmsbZcry: [],
      updateKey: false,
      selectedRow: null, // 保存当前选中的行
      // 子表选中数据
      checkedLxglXmsbYffyys: [],
      // 研发费用预算下表格数据
      lxglXmsbYffyysList: [],
      // 研发费用预算上表格数据
      lxglXmsbYffyList: [],
      // 直接用于研发的主要材料表格数据
      lxglXmsbYfclList: [],
      // 设备（专门用于研发主要仪器、设备清单）表格数据
      lxglXmsbYfsbList: [],
      // 中间试验费表格数据
      lxglXmsbZjsyfList: [],
      // 研究课题组成人员情况表格数据
      lxglXmsbZcryList: [],
      tbsmVisible: false, //填报说明弹窗
      lxglXmsbJhjdList: [], // 项目计划进度和目标表格数据
      dialogKey: 0, // 初始化 key 值
      deptOptions: undefined, // 部门树选项
      xmsbFileType: '', //附件文件类型(可研性研究报告........)
      modifyProject: false, //修改项目弹窗
      fjShangChuan: false, //附件上传弹窗
      xmsbIdEdit: "", //要修改的 项目id
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      fileList: [],
      headers: {Authorization: "Bearer " + getToken()},
      number: 0,
      uploadList: [],
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "docx", "zip", "jpg", "png"], // 上传附件要求的类型限制
      gnwgkAndFzqs: '',
      ktGs: '公司科研项目', //打开选择课题归属弹窗后的默认课题归属
      ktgsVisible: false, //选择课题归属弹窗
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedLxglXmsbFj: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目申报表格数据
      xmsbList: [],
      // 附件表格数据
      lxglXmsbFjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ktGs: null,
        year: null,
        ktName: null,
        ktBh: null,
        zrBm: null,
        ytGc: null,
        processStatus: null,
        deptId: null,
        userId: null,
      },
      // 表单参数
      form: {},
      // 列信息
      columns: [
        {key: 0, label: `年度`, visible: true}, {key: 1, label: `课题编号`, visible: true}, {
          key: 2,
          label: `课题名称`,
          visible: true
        },
        {key: 3, label: `课题归属`, visible: true}, {key: 4, label: `申报单位`, visible: true}, {
          key: 5,
          label: `姓名`,
          visible: true
        },
        {key: 6, label: `项目总额(万)`, visible: true}, {key: 7, label: `申请资助(万)`, visible: true}, {
          key: 8,
          label: `止时间`,
          visible: true
        },
        {key: 9, label: `申报日期`, visible: true}, {key: 10, label: `录入人员`, visible: true}
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //修改项目状态
    handleSubmit(type) {
      const row = this.selectedRow;
      if (row == null) {
        this.$modal.alertWarning("请先选中一条记录！");
      } else {
        // 打开遮罩层
        this.$modal.loading("正在修改项目状态，请稍后...");
        this.form = {
          xmsbId: row.xmsbId, xmZt: type
        };
        updateXmsb(this.form).then(response => {
          this.$modal.msgSuccess("修改项目状态成功！");
          this.getList();
          // 关闭遮罩层
          this.$modal.closeLoading();
        });
      }

    },
    //点击企业工法打开企业工法查看弹窗组件
    handleQyGfCk(row) {
      this.$refs.qyGfCk.handleOpen(row);
    },
    //点击专利申请打开专利申请查看弹窗组件
    handleZlSqCk(row) {
      this.$refs.zlSqCk.handleOpen(row);
    },
    //点击变更次数打开合同变更查看弹窗
    handleHtBgCk(row) {
      this.$refs.htBgCk.handleOpen(row);
    },
    //点击合同信息打开合同信息组件弹窗
    handleHtXxCk(row) {
      this.$refs.htXx.handleOpen(row);
    },
    //点击项目延期打开项目延期查看弹窗
    handleXmYqCk(row) {
      this.$refs.xmYqCk.handleOpen(row);
    },
    // 查询立项文件详情
    handlePzWhButtonClick(row) {
      this.$refs.lxWjXqPicture.handleOpen(row);
    },
    //查询进度
    handleJinDu(row) {
      this.$refs.picture.handleOpen(row);
    },
    // 导出按钮操作
    handleExport() {
      this.download('kylx/xmsb/export', {
        ...this.queryParams
      }, `xmsb_${new Date().getTime()}.xlsx`)
    },
    //导出word按钮操作
    handleExports() {
      const row = this.selectedRow; // 获取当前选中的行
      const queryParams = this.queryParams;
      const xmsbId = row.xmsbId || this.xmsbId; // Assuming lldId is defined somewhere in your component
      this.$confirm('是否确认导出word文档?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true
        return exportXmsbWord(queryParams, xmsbId); // Passing lldId to the exportWord function
      }).then(response => {
        this.$download.resource('/profile/upload/' + response.msg);
        this.loading = false
      }).catch(() => {
      });
    },
    // 查询子表信息
    handleButtonClick(row) {
      this.$refs.xmxq.handleOpen(row);
    },
    //选中当前点击的行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;
      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    // 查询项目申报列表
    getList() {
      this.loading = true;
      listXmsb(this.queryParams).then(response => {
        console.log(response.rows)
        this.xmsbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.xmsbId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },


  }
};
</script>

<style scoped>
/* header 头部背景 */
/deep/ .el-card__header {
  padding: 3px 7px 3px;
  min-height: 10px;
  background-color: aliceblue;
}
</style>
